<template>
  <a-modal
    :title="title"
    :width="900"
    :visible="visible"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    <a-form :form="form" :label-col="{ span: 5 }" :wrapper-col="{ span: 12 }">
      <a-form-item label="数据源名称">
        <a-input
          v-decorator="['dbName', { rules: [{ required: true, message: '请填写数据源名称！' }] }]"
        />
      </a-form-item>
      <a-form-item label="连接用户名">
        <a-input
          v-decorator="['dbUser', { rules: [{ required: true, message: '请填写连接用户名！' }] }]"
        />
      </a-form-item>
      <a-form-item label="连接密码">
        <a-input
          v-decorator="['dbPassword', { rules: [{ required: true, message: '请填写连接密码！' }] }]"
        />
      </a-form-item>
      <a-form-item label="驱动名称">
        <a-input
          v-decorator="['dbDriver', { rules: [{ required: true, message: '请填写驱动名称！' }] }]"
        />
      </a-form-item>
      <a-form-item label="连接URL">
        <a-input
          v-decorator="['dbUrl', { rules: [{ required: true, message: '请填写连接URL！' }] }]"
        />
      </a-form-item>
    </a-form>

    <div class="load" v-if="spinning == true" style="position: absolute;left:50%;top:50%;transform:translate(-50%,-50%);"><a-spin :spinning="spinning" tip="录入中..."></a-spin></div>

    <a-button @click="testConnct">测试连接</a-button>
  </a-modal>
</template>

<script>
import { httpAction, deleteAction, getAction } from '@/api/manage'
export default {
  name: 'Ureport2SetDBSourceModal',
  data(){
    return{
      title: "添加数据源",
      visible: false,
      spinning: false,
      form: this.$form.createForm(this),
      url: {
        updateUreport2DBSource:"/ureport2/ureport2/updateUreport2DBSource",
        testConnnction:"/ureport2/ureport2/testConnnction"
      }
    }
  },
  methods: {
    handleOk () {
      this.form.validateFields((err, values) => {
        this.spinning = true;
        let params = {}
        params.dbName = values.dbName
        params.dbUser = values.dbUser
        params.dbPassword = values.dbPassword
        params.dbDriver = values.dbDriver
        params.dbUrl = values.dbUrl
        getAction(this.url.updateUreport2DBSource,params).then((result)=>{
          if(result.success){
            this.spinning = false;
            this.$message.success("修改成功")
            this.visible = false;
          }
        })
      })
    },
    edit(record){
      this.visible = true;
    },
    handleCancel() {
      this.visible = false;
    },
    testConnct(){
      this.form.validateFields((err, values) => {
        let params = {}
        params.dbName = values.dbName
        params.dbUser = values.dbUser
        params.dbPassword = values.dbPassword
        params.dbDriver = values.dbDriver
        params.dbUrl = values.dbUrl
        getAction(this.url.testConnnction,params).then((result)=>{
          if(result.result){
            this.$message.success("连接成功")
          }else{
            this.$message.success("连接失败:" + result.error)
          }
        })
      })
    }
  }
}
</script>

<style scoped>

</style>